<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
    <h3>用户登陆效果</h3>
    <h4 style="color: red">{{error}}</h4>
    账号：<input v-model="username"/><br/>
    密码：<input type="password" v-model="password"/><br/>
    <button @click="login">登陆</button>
</div>
</body>
</html>
<script src="js/vue.min.js"></script>
<script src="js/axios.min.js"></script>
<script>
    new Vue({
        el: '#app',
        data() {
            return {
                username: '',
                password: '',
                error: ''
            }
        },
        methods: {
            login() {
                let data = new URLSearchParams()
                data.append('username', this.username)
                data.append('password', this.password)
                data.append('method', 'login')
                axios({
                    method: 'post',
                    url: 'user',
                    data: data
                }).then(resp => {
                    if (resp.data.success) {
                        // 1.获取到用户信息
                        let user = resp.data.data
                        console.log(user)
                        localStorage.setItem('username',user.username)
                        location.href = 'index.html'
                    } else {
                        this.error = resp.data.error
                    }
                })
            }
        }
    })
</script>